<template>
  <div class="news">
    <el-row :gutter="20">
      <el-col :span="24">
        <el-card class="box-card">
          <div class="boxcard-head">
            <el-form :model="queryParam" ref="queryParam" inline class="demo-form-inline">
              <el-form-item label="名称" prop="name">
                <el-input placeholder="名称" v-model="queryParam.name"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="getList">查询</el-button>
                <el-button @click="resetForm('queryParam')">重置</el-button>
                <el-button type="primary" icon="el-icon-plus" @click="dialogAdd()">添加</el-button>
              </el-form-item>
            </el-form>
          </div>
          <div class="boxcard-body">
            <el-table
              :data="list"
              style="width: 100%"
              row-key="id"
              :header-cell-style="{ 'background-color': '#EEEEEE' }"
            >
              <el-table-column type="index" label="排序" width="60"></el-table-column>
              <el-table-column prop="imageUrl" label="图片">
                <template slot-scope="scope">
                  <el-image
                    :src="scope.row.imageUrl"
                    style="width:100px;height:60px"
                    :preview-src-list="[scope.row.imageUrl]"
                  />
                </template>
              </el-table-column>
              <el-table-column prop="title" label="标题" show-overflow-tooltip></el-table-column>
              <el-table-column prop="description" label="简介" show-overflow-tooltip></el-table-column>
              <el-table-column prop="updatedDate" label="更新时间" show-overflow-tooltip></el-table-column>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button @click="dialogField(scope.row)" type="text" size="small">编辑</el-button>
                  <el-button
                    @click="changeStatus(scope.row)"
                    type="text"
                    size="small"
                  >{{scope.row.status==1?"下架":"发布"}}</el-button>
                  <el-button @click="delectField(scope.row)" type="text" size="small">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页 -->
            <div class="pagination">
              <el-pagination
                background
                @size-change="handleSizeChange"
                @current-change="pageChange"
                :total="total"
                :current-page.sync="pageNum"
                :page-sizes="[10, 20]"
                :page-size="10"
                layout="total, sizes, prev, pager, next"
              ></el-pagination>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <!-- 弹窗 -->
    <div>
      <el-dialog
        :title="editName"
        :visible.sync="dialogFieldBox"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        width="60%"
        z-index="3"
      >
        <div class="eldialogdiv">
          <el-form
            :model="news"
            :rules="rules"
            label-position="right"
            label-width="100px"
            ref="form"
          >
            <el-form-item label="标题" prop="title">
              <el-input v-model="news.title" />
            </el-form-item>
            <el-form-item label="简介" prop="description">
              <el-input v-model="news.description" />
            </el-form-item>
            <el-form-item label="图片" prop="imageUrl">
              <singleImgUpload :url.sync="news.imageUrl" dir="newsImage"></singleImgUpload>
            </el-form-item>
            <el-form-item label="内容" prop="content">
              <!-- <richTextEditor v-bind:text="news.content" v-on:editor-change="editorChange"></richTextEditor> -->
              <tinymce v-if="dialogFieldBox" ref="editor" v-model="news.content" :disabled="false" />
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer" style="text-align: right;">
            <el-button @click="cancelClick">取 消</el-button>
            <el-button type="primary" @click="confirmClick">确 定</el-button>
          </div>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { get, postJson, postXform } from "@/utils/request.js";
import singleImgUpload from "../../components/upload/SingleImgUpload.vue";
import richTextEditor from "@/components/common/richTextEditor";
import tinymce from "@/components/common/myTinymce.vue";

export default {
  components: { singleImgUpload, richTextEditor, tinymce },
  data() {
    return {
      queryParam: {
        name: null
      },
      // 分页参数
      pageNum: 1, // 当前页数
      pageSize: 10, // 页大小
      total: 0, // 数据总条数
      list: [], // 数据列表
      // 弹窗相关属性
      editName: "编辑",
      dialogFieldBox: false,
      // 弹窗数据
      news: {
        id: null,
        title: null,
        description: null,
        imageUrl: [],
        content: null
      },
      // 表单校验
      rules: {
        title: [
          {
            required: true,
            message: "名称不能为空",
            trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
    this.getList(this.pageNum, this.pageSize);
  },
  watch: {
    // dialogFieldBox: {
    // 	handler() {
    // 		if (!this.dialogFieldBox) {
    // 			this.news.imageUrl = '';
    // 		}
    // 	},
    // 	immediate: true
    // }
  },
  methods: {
    // 分页相关方法
    // 页大小改变时
    handleSizeChange(val) {
      this.pageSize = val;
    },
    // 页码改变时
    pageChange(e) {
      // 获取某页数据
      this.pageNum = e;
      this.getList();
    },
    // 获取列表数据
    getList(num, size) {
      get("/news/pageNews", {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        name: this.queryParam.name
      }).then(res => {
        this.list = res.data.data.list;
        // 自定义下标
        this.list.forEach((e, idx) => {
          e.idx = (idx + 1) * this.pageNum;
        });
        this.total = Number.parseInt(res.data.data.total);
        this.pageNum = res.data.data.pageNum;
      });
    },
    // 删除当前新闻
    delectField(e) {
      this.$confirm("是否删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          postXform("news/deleteNews", { id: e.id }).then(res => {
            this.$message({ message: "删除成功", type: "success" });
            this.getList();
          });
        })
        .catch(() => {});
    },
    dialogAdd() {
      this.editName = "新增";
      this.dialogFieldBox = true;
      this.news = {
        id: null,
        title: null,
        description: null,
        imageUrl: null,
        content: null
      };
    },
    // 编辑按钮打开编辑字段弹窗
    dialogField(e) {
      this.editName = "编辑";
      get("/news/getNews", { id: e.id }).then(res => {
        this.news = {
          id: res.data.data.id,
          title: res.data.data.title,
          description: res.data.data.description,
          imageUrl: res.data.data.imageUrl,
          content: res.data.data.content
        };
        this.dialogFieldBox = true;
      });
    },
    // 确认保存
    confirmClick() {
      const vm = this;
      let param = { ...this.news };
      this.$refs.form.validate(valid => {
        if (valid) {
          if (!this.news.id) {
            postXform("/news/addNews", param).then(res => {
              this.$message({ message: "操作成功", type: "success" });
              this.dialogFieldBox = false;
              this.getList();
              this.news = {
                id: null,
                title: null,
                description: null,
                imageUrl: null,
                content: null
              };
            });
          } else {
            postXform("/news/updateNews", param).then(res => {
              this.$message({ message: "操作成功", type: "success" });
              this.dialogFieldBox = false;
              this.getList();
              this.news = {
                id: null,
                title: null,
                description: null,
                imageUrl: null,
                content: null
              };
            });
          }
        } else {
          return false;
        }
      });
    },
    cancelClick() {
      this.dialogFieldBox = false;
      this.$refs.form.clearValidate();
      this.news = {
        id: null,
        title: null,
        description: null,
        imageUrl: null,
        content: null
      };
    },
    editorChange(html) {
      this.news.content = html;
    },
    changeStatus(row) {
      postXform("news/status", { id: row.id }).then(res => {
        this.$message({ message: "操作成功", type: "success" });
        if (row.status == 1) {
          row.status = 0;
        } else {
          row.status = 1;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      if (formName == "queryParam") {
        this.getList();
      }
    }
  }
};
</script>

<style lang="less" scoped="scoped">
.eldialogdiv {
  overflow-y: scroll;
  height: 70vh;
}
</style>
